package com.hnkywl.sts.service.tjbb;


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hnkywl.sts.dao.tjbb.SylbbDao;
import com.hnkywl.sts.dao.tjbb.YjbbDao;
import com.hnkywl.sts.dao.xsgl.KhDao;
import com.hnkywl.sts.entity.fkyj.Syl;
import com.hnkywl.sts.entity.organ.Qyjg;
import com.hnkywl.sts.entity.xsgl.Kh;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.orm.hibernate.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: SDE09
 * Date: 13-9-28
 * Time: 下午5:13
 * To change this template use File | Settings | File Templates.
 */
@Service
public class YjbbService {

    private YjbbDao yjbbDao;
    private SylbbDao sylbbDao;


    @Autowired
    public void setYjbbDao(YjbbDao yjbbDao) {
        this.yjbbDao = yjbbDao;
    }

    @Autowired
    public void setSylbbDao(SylbbDao sylbbDao) {
        this.sylbbDao = sylbbDao;
    }

    public String findKhkctjList(String kh, Long cggsid, String code, String name, String ggxh, String sort, String order, int pageNo, int pageSize) {
        Gson gson = new Gson();
        Page<Map> khkctjPage = this.yjbbDao.findKhkctjList(kh, cggsid, code, name, ggxh, sort, order, pageNo, pageSize);
        Map sumKhkctj = yjbbDao.khkctjHj(kh, cggsid, code, name, ggxh);
        String json = gson.toJson(khkctjPage.getResult());
        return "{\"total\":" + khkctjPage.getTotalCount() + ",\"rows\":" + json + ",\"footer\":[{\"czggxh\":\"合计:\",\"jl\":" + sumKhkctj.get("jl") + ",\"hz\":" + sumKhkctj.get("hz") + "}]}";
    }

    public String findKhkhztjList(String kh, Long cggsid, String code, String name, String ggxh, Date khkhztjrq, String sort, String order, int pageNo, int pageSize) {
        Gson gson = new Gson();
        Page<Map> khkhztjPage = this.yjbbDao.findKhkhztjList(kh, cggsid, code, name, ggxh, khkhztjrq, sort, order, pageNo, pageSize);
        Map sumKhkhztj = yjbbDao.khkhztjHj(kh, cggsid, code, name, ggxh, khkhztjrq);
        String json = gson.toJson(khkhztjPage.getResult());
        return "{\"total\":" + khkhztjPage.getTotalCount() + ",\"rows\":" + json + ",\"footer\":[{\"czggxh\":\"合计:\",\"jl\":" + sumKhkhztj.get("jlhj") + ",\"khz\":" + sumKhkhztj.get("khzhj") + "}]}";
    }

    public String findSyltjList(String kh, Float syl, Date jsrqStart, Date jsrqEnd, int pageNo, int pageSize) {
        Page<Map> page = this.yjbbDao.findSyltjList(kh, syl, jsrqStart, jsrqEnd, pageNo, pageSize);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String sskcJson = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + sskcJson + "}";
    }

    public List findKhkctjChart(Long khChart, String codeChart, String nameChart, String ggxhChart, Long cggsidChart, String tj) {
        return this.yjbbDao.findKhkctjChart(khChart, codeChart, nameChart, ggxhChart, cggsidChart, tj);
    }


    public List findKhkhztjChart(String codeChart, String nameChart, String ggxhChart, Long khId, Long cggsidChart, String data) {
        return this.yjbbDao.findKhkhztjChart(codeChart, nameChart, ggxhChart, khId, cggsidChart, data);
    }

    public Qyjg findSyltjKh(Long khChart) {
        return this.yjbbDao.findSyltjKh(khChart);
    }


    public List findKhSyl(Long sbgsid, String date) {
        return this.yjbbDao.findKhSyl(sbgsid, date);
    }

    public Syl findSylDetail(Long sylId) {
        return this.sylbbDao.get(sylId);
    }

    public List findKhYjz(Long cggsidChart) {
        return this.yjbbDao.findKhYjz(cggsidChart);
    }

    public String findAllQyjgSyl(String qcName, String jcName, int pageNo, int pageSize) {
        Page<Map> page = this.yjbbDao.findSyltjList(qcName, jcName, pageNo, pageSize);
        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
        String sskcJson = gson.toJson(page.getResult());
        return "{\"total\":" + page.getTotalCount() + ",\"rows\":" + sskcJson + "}";
    }

    public void updateSylwh(Long khId, Float jzsyl) {
        String selSql = "select * from t_sys_jzsyl j where j.khid = ? ";
        List list = this.yjbbDao.findBySQL(selSql, khId);
        if (null != list && list.size() > 0) {
            String updateSql = " update t_sys_jzsyl jz set jz.jzsyl = ?,jz.last_updated = sysdate  where jz.khid = ?  ";
            this.yjbbDao.executeSQL(updateSql, jzsyl, khId);
        } else {
            String saveSql = " insert into t_sys_jzsyl(id,date_created,deleted,last_updated,createuser_id,createorgan_id,createdepartment_id,jzsyl,khid) " +
                    " values (seq_sys_jzsyl.nextval,sysdate,0,sysdate,?,?,?,?,?)";
            Long createOrganId = null;
            Long departmentmenetId = null;
            if (null != YgService.getCurrentYg().getCreateOrgan()) {
                createOrganId = YgService.getCurrentYg().getCreateOrgan().getId();
            } else {
                createOrganId = YgService.getCurrentYgGsId();
            }
            if (null != YgService.getCurrentYg().getCreateDepartment()) {
                departmentmenetId = YgService.getCurrentYg().getCreateDepartment().getId();
            } else {
                departmentmenetId = YgService.getCurrentYgGsId();
            }
            this.yjbbDao.executeSQL(saveSql, YgService.getCurrentYg().getId(), createOrganId, departmentmenetId, jzsyl, khId);
        }
    }

    public List<Map> findExportQyjgSyl() {
        return this.yjbbDao.findExportQyjgSyl();
    }

    public void saveOrUpdateSylwh(List<Map<String, String>> listMap) {
        for (int i = 0; i < listMap.size(); i++) {
            Long khId = Long.parseLong(listMap.get(i).get("qjgyId"));
            Float jzsyl = Float.parseFloat(listMap.get(i).get("jzsyl"));
            String sql = " select * from t_sys_jzsyl where khid = ? ";
            List list = this.yjbbDao.findBySQL(sql, khId);
            if (null != list && list.size() > 0) {
                String updateSql = " update t_sys_jzsyl jz set jz.jzsyl = ?,jz.last_updated = sysdate  where jz.khid = ?  ";
                this.yjbbDao.executeSQL(updateSql, jzsyl, khId);
            } else {
                String saveSql = " insert into t_sys_jzsyl(id,date_created,deleted,createuser_id,createorgan_id,createdepartment_id,jzsyl,khid) " +
                        " values (seq_sys_jzsyl.nextval,sysdate,0,?,?,?,?,?)";
                Long createOrganId = null;
                Long departmentmenetId = null;
                this.yjbbDao.executeSQL(saveSql, YgService.getCurrentYg().getId(), YgService.getCurrentYg().getZggs().getId(), YgService.getCurrentYg().getZgjg().getId(), jzsyl, khId);
            }
        }
    }

    public List<Map> findQyjgSylByKhId(Long khId) {
        return this.yjbbDao.findQyjgSylByKhId(khId);
    }

    public List findKhForKhz(Long cggsidChart) {
        String sql = "select kh.sbgs_id,g.qc  from t_biz_kh kh ,t_sys_gsjg g where kh.deleted =0 and kh.qk = 0 and  " +
                " kh.sbgs_id =g.id and kh.cggs_id = ? group by kh.sbgs_id,g.qc";
        Long cggsId = 0L;
        if (cggsidChart != null) {
            cggsId = cggsidChart;
        } else {
            cggsId = YgService.getCurrentYgGsId();
        }
        return this.yjbbDao.findBySQL(sql, cggsId);
    }

    public List findKhForSyl() {
        String sql = " select s.khid,g.qc from t_biz_syl s left join t_sys_gsjg g  on s.khid = g.id   group by khid,qc ";
        return this.yjbbDao.findBySQL(sql);
    }


    public List findKhSyltjChart(Long khid, String data) {
        String sql = " select s.khid,decode(s.syl,null,0,s.syl) from t_biz_syl s where s.khid = ? and to_char(s.jsrq,'yyyyMMdd') = ? ";
        return this.yjbbDao.findBySQL(sql,khid,data);
    }
}
